package com.hzy.framework;

/**
 * <p>
 * ApiResult Ajax 返回 对象
 * </p>
 *
 * @author hzy
 * @since 2020-04-26
 */
public class ApiResult {

    public ApiResult(ApiStatusCodeEnum statusCodeEnum, String message, Object data) {
        this.code = statusCodeEnum.getValue();
        this.message = message;
        this.data = data;
    }

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 消息
     */
    private String message;
    /**
     * 数据
     */
    private Object data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    /**
     * ApiResult status 的枚举值
     */
    public enum ApiStatusCodeEnum {
        /**
         * 接口不存在
         */
        NotFount(-3),
        /**
         * 程序异常
         */
        Error(-2),
        /**
         * 未授权
         */
        UnAuth(-1),
        /**
         * 警告
         */
        Warn(0),
        /**
         * 成功
         */
        Ok(1);

        ApiStatusCodeEnum(int value) {
            this.value = value;
        }

        private final Integer value;

        public Integer getValue() {
            return this.value;
        }
    }

    /**
     * 返回
     *
     * @param code
     * @param message
     * @return ApiResult
     */
    public static ApiResult result(ApiStatusCodeEnum code, String message) {
        return new ApiResult(code, message, null);
    }

    /**
     * 返回
     *
     * @param code
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(ApiStatusCodeEnum code, Object data) {
        return new ApiResult(code, "result", data);
    }

    /**
     * 返回
     *
     * @param code
     * @param message
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(ApiStatusCodeEnum code, String message, Object data) {
        return new ApiResult(code, message, data);
    }

    /**
     * 成功
     *
     * @return ApiResult
     */
    public static ApiResult ok() {
        return ApiResult.result(ApiStatusCodeEnum.Ok, "success", null);
    }

    /**
     * 成功
     *
     * @param message 消息
     * @return ApiResult
     */
    public static ApiResult okMessage(String message) {
        return ApiResult.result(ApiStatusCodeEnum.Ok, message);
    }

    /**
     * 成功
     *
     * @param message
     * @param data
     * @return ApiResult
     */
    public static ApiResult ok(String message, Object data) {
        return ApiResult.result(ApiStatusCodeEnum.Ok, message, data);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @return ApiResult
     */
    public static ApiResult ok(Object data) {
        return ApiResult.result(ApiStatusCodeEnum.Ok, "success", data);
    }

    /**
     * 警告
     *
     * @return ApiResult
     */
    public static ApiResult warn() {
        return ApiResult.result(ApiStatusCodeEnum.Warn, "warn");
    }

    /**
     * 警告
     *
     * @param message
     * @return ApiResult
     */
    public static ApiResult warnMessage(String message) {
        return ApiResult.result(ApiStatusCodeEnum.Warn, message);
    }

    /**
     * 警告
     *
     * @param message
     * @param data
     * @return ApiResult
     */
    public static ApiResult warn(String message, Object data) {
        return ApiResult.result(ApiStatusCodeEnum.Warn, message, data);
    }

    /**
     * 警告
     *
     * @param data
     * @return ApiResult
     */
    public static ApiResult warn(Object data) {
        return ApiResult.result(ApiStatusCodeEnum.Warn, "warn", data);
    }


}
